<template>
<div class="riliday-wrapper">
  <div class="ui items" :class="{notThisMonth:!day.isThisMonth}">
    <div class="item">{{day.show}}
      <i @click.stop.prevent="dayEdit(day)" class="icon edit right"></i>
    </div>
    <div class="item" v-for="field in fieldNames">
      <span v-show="false">{{fname=field.name}}</span>
      <input readonly v-if="dayData" type="text" :value="dayData[fname]">
      <input readonly v-else type="text" value="">
    </div>
  </div>
</div>
</template>
<script type="text/ecmascript-6">
  import tool from 'src/util/tool'
  import logic from '_logic';
  import util from '_util/index';

  export default {
    data() {
      return {
        dayData: {},
        data_emit: null,
      }
    },
    created() {
      this.dayData =this.data_emit = this._dayData;
    },
    mounted() {

    },
    computed: {
      mid() {
        return  this.modelInfo.riliDayMid;
      },
      loading() {
        return !(this.group);
      },
      enum_val_ol(){
        let fname=this.mid == 55 ? 'ol' : 'is_valid';
        let field=this.fieldNames.find(field=>{
          return field.name == fname;
        })
        return field.enum_val||null;
      }
    },
    methods: {
      setDayData(vo){
        switch (this.mid) {
          case 55:
            this.setDayData_product(vo);
            break;
          case 60:
            this.setDayData_whprod(vo);
            break;
        }
      },
      setDayData_product(vo){
        this.dayData=this.dayData||{}
        this.$set(this.dayData,'quantity',vo['quantity'])
        if(this.enum_val_ol)
          this.$set(this.dayData,'ol',this.enum_val_ol[vo['ol']])
        this.$set(this.dayData,'ol_real',vo['ol'])
      },
      setDayData_whprod(vo){
        this.dayData = this.dayData || {};

//        if(vo['sale_price']<(vo['fc_serv_up']+vo['fc_serv']+vo['fc_sp'])) {
//          util.showMsg(this, 'quick_error', '分成总额大于售价！');
//          return;
//        }

        this.$set(this.dayData, 'sale_price', vo['sale_price']);
        this.$set(this.dayData, 'fc_serv_up', vo['fc_serv_up']);
        this.$set(this.dayData, 'fc_serv', vo['fc_serv']);
        this.$set(this.dayData, 'fc_sp', vo['fc_sp']);

        if(this.enum_val_ol)
          this.$set(this.dayData,'is_valid',this.enum_val_ol[vo['is_valid']])
        this.$set(this.dayData,'is_valid_real',vo['is_valid'])
      },
      dayEdit(day){
        util.store.commit(this,'setModalOp', this.dayData?'update':'insert');
        let mid = this.mid;

        let _vo = this.getDataCopy();
        logic.edit.showEditPop(this, mid, ()=> {

          let vo = util.store.get(this, 'editVoModal');
          this.setDayData(vo);

          this.toEmit();

        },_vo);
      },
      toEmit(){
        let tmp = this.getDataCopy();
        this.$emit('setDayData', this.day.show, tmp);
      },
      getDataCopy(){
        let tmp=util.deepCopy(this.dayData)
        if(this.mid==55) {
          tmp.ol=tmp.ol_real
          delete tmp.ol_real
        }else{
          tmp.is_valid=Number(tmp.is_valid_real)
          delete tmp.is_valid_real
        }

        return tmp;
      }
    },
    watch: {
//      data_emit: function (val, old) {
//        if(val) {
//          this.$emit('setDayData', this.day.show, val);
//        }
//      },
      _dayData: function (val, old) {
        if(val) {
          this.setDayData(this._dayData)
        }
      }
//      dayData: function (val, old) {
//        if (val) {
//          let tmp=util.deepCopy(val)
//          tmp.ol=tmp.ol_real
//          delete tmp.ol_real
//          this.$emit('setDayData', this.day.show, tmp)
//        }
//      }
    },
    components: {},
    props: [ 'day','_dayData','fieldNames','fieldNames','modelInfo']
  }
</script>
<style rel="stylesheet/less" scoped lang="less">@dir: "common/";
.riliday-wrapper{

  .icon.edit.right {
    margin-left: 15px;
    cursor: pointer;
    color: rgba(0,0,0,0.6);
    &:hover{
      color: rgba(0,0,0,0.9);
    }
  }
  .ui.items {
    input {
      &:focus{
        background-color: transparent;
      }
      border: none;
      background-color: transparent;
    }
  }
  .notThisMonth {
    background-color: #ccc;
  }
}
</style>
